<template>

    <!-- 生日选择 -->
    <van-datetime-picker
        v-model="currentDate"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="$emit('close')"
        @confirm="onConfirm"
    />

  </template>
  
  <script>
  import { updateUserProfile } from '@/api/user'
  import dayjs from 'dayjs'
  
  export default {
      name: 'UpdateBirthday',
      props: {
          value: {
              type: String,
              required: true
          }
      },
      data() {
          return {
            minDate: new Date(1920, 0, 1),
            maxDate: new Date(),
            currentDate: new Date(this.value)
          }
      },
      methods: {
        async onConfirm() {
            this.$toast.loading({
                message: '保存中...',
                duration: 0, // 持续展示 toast
                forbidClick: true // 禁用背景点击
            })
              try {
                // 转时间格式
                const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
                
                await updateUserProfile({
                    birthday: currentDate
                })

                // 更新视图
                this.$emit('input', currentDate)
                // 关闭弹出层
                this.$emit('close')
                // 成功提示
                this.$toast.success('修改生日成功')
            } catch (err) {
                this.$toast.fail('操作失败')
            }
        }
      }
  
  }
  </script>
  
  <style scoped lang="less">
  
  </style>